﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web.Mvc;
using CMSMVC4.Areas.Admin.Models;
using CMSMVC4.Filters;

namespace CMSMVC4.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin,Comment")]
    public class CommentController : Controller
    {
        //
        // GET: /Admin/Comment/
        readonly CMSMVC4Entities _db = new CMSMVC4Entities();

        public ActionResult Detail(string id)
        {
            int idcm;
            int.TryParse(id, out idcm);
            Coment cm = _db.Coments.FirstOrDefault(x => x.Id == idcm);
            return View(cm);
        }

        public ActionResult Confirm()
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Comment/Confirm").Rows[0]["Parrent"];
            var data = _db.Coments.Where(x => x.Status == CommentStatus.Confirm).ToList();
            return View(data);
        }

        [HttpPost]
        public ActionResult Confirm(List<CommentComfirmModel> model)
        {
            foreach (var row in from coment in model where coment.status select _db.Coments.FirstOrDefault(x => x.Id == coment.id))
            {
                row.Status = CommentStatus.Confirm;
            }
            _db.SaveChanges();
            return RedirectToAction("NotConfirm");
        }

        public ActionResult NotConfirm()
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Comment/NotConfirm").Rows[0]["Parrent"];
            var data = _db.Coments.OrderByDescending(x=>x.Id).Where(x => x.Status == CommentStatus.NotConfirm).ToList();
            var listcm = new List<CommentComfirmModel>();
            foreach (var coment in data)
            {
                var cf = new CommentComfirmModel();
                cf.id = coment.Id;
                cf.email = coment.Email;
                cf.data = coment.Data;
                cf.idpage = coment.Page;
                cf.status = coment.Status == CommentStatus.Confirm;
                listcm.Add(cf);
            }
            return View(listcm);
        }


        //
        // GET: /Admin/Comment/Delete
        #region

        [HttpPost]
        public JsonResult Delete(string[] id)
        {
            if (id != null)
            {
                foreach (var i in id)
                {
                    int idcomment;
                    int.TryParse(i, out idcomment);
                    var row = _db.Coments.FirstOrDefault(x => x.Id == idcomment);
                    _db.Coments.DeleteObject(row);
                    _db.SaveChanges();
                }
            }
            return Json(string.Empty, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
